<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="零配件名称:">
        <el-input v-model="queryParams.skuName" size="small" placeholder="请输入零配件名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-popover placement="bottom-start" width="460" trigger="click">
          <sku-selector ref="skuSelector" :query-params="{customerId: customer.id}" @selected="handleSelectedSku($event)" />
          <el-button slot="reference" type="primary" icon="el-icon-plus" size="mini">选择</el-button>
        </el-popover>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row>
      <el-table-column label="客户名称" align="center" prop="customerName" width="180" />
      <el-table-column label="物料名称" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.materialNo" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.materialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算类型" align="center" prop="chargeType" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-select v-model="row.shipByPackOrQty" style="width: 100%;" clearable>
              <el-option v-for="option in $constant.shipByPackOrQtyOptions" :key="option.key" :label="option.label" :value="option.key" />
            </el-select>
          </template>
          <span v-else>{{ row.shipByPackOrQty }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单个重量" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.unitWeight" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.unitWeight }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单个体积" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.unitVolume" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.unitVolume }}</span>
        </template>
      </el-table-column>
      <el-table-column label="托盘最大装载包装数" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.packPerPallet" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.packPerPallet }}</span>
        </template>
      </el-table-column>
      <el-table-column label="包装件数" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.qtyPerPack" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.qtyPerPack }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库是否要拆分" align="center" prop="chargeType" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-radio-group v-model="row.needrRePackYn">
              <el-radio v-for="option in $constant.ynOptions" :key="option.key" :label="option.label">{{ option.label }}</el-radio>
            </el-radio-group>
          </template>
          <span v-else>{{ row.needrRePackYn }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要托盘" align="center" prop="chargeType" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-radio-group v-model="row.needPalletYn">
              <el-radio v-for="option in $constant.ynOptions" :key="option.key" :label="option.label">{{ option.label }}</el-radio>
            </el-radio-group>
          </template>
          <span v-else>{{ row.needPalletYn }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要叉车" align="center" prop="chargeType" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-radio-group v-model="row.needLiftYn">
              <el-radio v-for="option in $constant.ynOptions" :key="option.key" :label="option.label">{{ option.label }}</el-radio>
            </el-radio-group>
          </template>
          <span v-else>{{ row.needLiftYn }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed>
        <template slot-scope="{row}">
          <el-button v-if="row.edit" type="success" size="small" icon="el-icon-circle-check-outline" @click="handleEdit(row)">确认</el-button>
          <el-button v-else type="primary" size="small" icon="el-icon-edit" @click="row.edit=!row.edit">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import TWhWarehouseCustomerMaterialApi from '@/api/pms/TWhWarehouseCustomerMaterialApi'
import SkuSelector from '@/views/goods/sku/SkuSelector.vue'

const defaultFormData = {
  customerId: null,
  customerName: null,
  materialNo: null,
  shipByPackOrQty: null,
  unitWeight: null,
  unitVolume: null,
  packPerPallet: null,
  qtyPerPack: null,
  needrRePackYn: null,
  needPalletYn: null,
  needLiftYn: null
}
export default {
  name: 'ChargeConfigList',
  components: {
    SkuSelector
  },
  props: {
    customer: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  watch: {
    customer() {
      this.$refs.skuSelector.handleQuery()
      this.handleQuery()
    }
  },
  data() {
    return {
      options: {
        materialOptions: [],
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        customerNo: this.customer.id,
        skuName: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      loading: true,
      searchVisible: true,
      skuSelectorVisible: false
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.page.records.push({
        ...defaultFormData,
        customerId: this.customer.id,
        customerName: this.customer.customerName,
        edit: true
      })
    },
    handleRemove(row) {
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return TWhWarehouseCustomerMaterialApi.remove(row.id)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      if (row.id) {
        TWhWarehouseCustomerMaterialApi.edit(row).then(response => {
          this.$message.success('修改成功')
          this.handleQuery()
        })
      } else {
        TWhWarehouseCustomerMaterialApi.add(row).then(response => {
          this.$message.success('添加成功')
          this.handleQuery()
        })
      }
    },
    handleQuery() {
      this.loading = true
      if (this.customer.id) {
        TWhWarehouseCustomerMaterialApi.page(this.queryParams, this.page).then(response => {
          this.page.records = response.data.records
          this.page.total = response.data.total
          this.loading = false
        })
      }
    },
    handleSelectedSku(item) {
      this.page.records.push({
        materialNo: item.skuCode,
        shipByPackOrQty: null,
        unitWeight: null,
        unitVolume: null,
        packPerPallet: null,
        qtyPerPack: null,
        needrRePackYn: null,
        needPalletYn: null,
        needLiftYn: null,
        customerId: this.customer.id,
        customerName: this.customer.customerName,
        edit: true
      })
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
